package work.hashmap;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Problem1 {
//    public int[] twoSum(int[] nums, int target) {
//        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
//        for (int i = 0; i < nums.length; ++i) {
//            if (hashtable.containsKey(target - nums[i])) {
//                return new int[]{hashtable.get(target - nums[i]), i};
//            }
//            hashtable.put(nums[i], i);
//        }
//        return new int[0];
//
//    }

    public static void main(String[] args) {
        Problem1 wdh = new Problem1();
        int nums[] = {2, 7, 11, 5};
        int target = 16;
        System.out.println(Arrays.toString(wdh.twoSum(nums, target)));

    }

    //11月复习
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(target - nums[i])) {
                map.put(nums[i], i);
            } else {
                return new int[]{i, map.get(target-nums[i])};
            }
        }
        return new int[]{0};
    }
}
